<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>首页</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" type="text/css" href="/css/bootstrap.css">    <!-- 自定义样式 -->
    <link rel="stylesheet" type="text/css" href="/css/index.css">    <!-- 自定义样式 -->
</head>

<body>
<div class="A1">
    <header class="B1">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <div class="C1">
                        <nav class="navbar navbar-expand-lg navbar-light bg-light">
                            <a class="navbar-brand" href="#">[[${session.users.username}]]</a>
                            <button class="navbar-toggler collapsed" type="button" data-bs-toggle="collapse"
                                    data-bs-target="#navbarNav"
                                    aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                                <span class="navbar-toggler-icon"></span>
                            </button>
                            <div class="navbar-collapse collapse" id="navbarNav">
                                <ul class="navbar-nav ml-auto">
                                    <li class="nav-item active">
                                        <a class="nav-link active" href="/index">首页</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" onclick="redirectToBooks()">书籍</a>
                                    </li>
                                    <li class="nav-item">
                                        <a class="nav-link" href="/changePassword">修改密码</a>
                                    </li>
                                    <li class="nav-item logout">
                                        <a class="nav-link" onclick="logout()">退出登录</a>
                                    </li>
                                </ul>
                            </div>
                        </nav>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <div class="B2">
        <div class="container">
            <table class="table table-striped table-bordered table-hover">
                <thead>
                <tr>
                    <th>id</th>
                    <th>账号</th>
                    <th>邮箱</th>
                    <th>修改时间</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="user:${session.user}">
                    <td th:text="${user.id}"></td>
                    <td th:text="${user.username}"></td>
                    <td th:text="${user.email}"></td>
                    <td th:text="${#temporals.format(user.updateTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
                    <td>
                        <button class="btn btn-primary" onclick="handleEdit(this)"
                                th:data-user-id="${user.id}"
                                th:data-username="${user.username}"
                                th:data-email="${user.email}">编辑
                        </button>
                        <button class="btn btn-danger" onclick="handleDelete(this)"
                                th:data-username="${user.username}"
                                th:data-name="${session.users.username}">删除
                        </button>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <!-- 编辑用户的模态 -->
        <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="editModalLabel">Edit User</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form id="editUserForm">
                            <div class="mb-3">
                                <label for="edit-id" class="form-label">ID</label>
                                <input type="text" class="form-control" id="edit-id" disabled>
                            </div>
                            <div class="mb-3">
                                <label for="edit-username" class="form-label">账号</label>
                                <input type="text" class="form-control" id="edit-username" disabled>
                            </div>
                            <div class="mb-3">
                                <label for="edit-email" class="form-label">邮箱</label>
                                <input type="email" class="form-control" id="edit-email" required
                                       pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$">
                                <div class="invalid-feedback">
                                    请输入有效的电子邮箱地址。
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-primary" onclick="saveChanges()">确定</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- 删除确认模态框 -->
        <div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-labelledby="deleteConfirmModalLabel"
             aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="deleteConfirmModalLabel">确认删除</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        确定要删除该用户吗？
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                        <button type="button" class="btn btn-danger" id="confirmDeleteButton">删除</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Bootstrap JS -->
<script src="/js/jquery3.7.1.js"></script>
<!--<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>没用到，
用于处理弹出框、下拉菜单等组件的定位和偏移-->
<script src="/js/bootstrap5.3.0.js"></script>
<script src="/js/axios.min.js"></script>
<script src="/js/index.js"></script>
</body>
</html>